package com.onegene.pdf.component.paint;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.layout.Canvas;
import com.itextpdf.layout.element.Image;
import com.onegene.pdf.component.factory.ReportBuilderContext;
import com.onegene.pdf.component.report.cancer.CancerColor;
import com.onegene.pdf.component.report.gene.GenoColor;
import com.onegene.pdf.util.Precondition;
import org.springframework.util.StringUtils;

import java.net.MalformedURLException;

/**
 * @author: laoliangliang
 * @description:
 * @create: 2020/7/3 16:23
 **/
public class CancerPainting extends AbstractPainting {
    public CancerPainting(PdfDocument pdf, PdfFont font) {
        super(pdf, font);
    }

    /**
     * 画竖线及横线
     */
    public void drawHeaderLine(boolean hasDashed) {
        pdfCanvas.saveState();
        pdfCanvas.setStrokeColor(CancerColor.getThemeColor());
        pdfCanvas.setLineWidth(1f);
        float y = pageSize.getTop() - 80;
        float left = 55;
        pdfCanvas.moveTo(left, y)
                .lineTo(pageSize.getWidth() - left, y).stroke();
        if (hasDashed) {
            float lineX = 109;
            pdfCanvas.moveTo(pageSize.getWidth() - lineX, y + 5)
                    .setLineDash(2.5f, 0.5f).lineTo(pageSize.getWidth() - lineX, y + 35).stroke();
        }
        pdfCanvas.restoreState();
    }

    public void drawHeader() {
        pdfCanvas.setStrokeColor(GenoColor.getThemeColor());
        pdfCanvas.setLineWidth(0.7f);
        // 画头线（在开头画会使整个页面画不出来）
        float y = pageSize.getTop() - 40;
        float xOffset = 55;
        pdfCanvas.moveTo(xOffset, y)
                .setLineWidth(1.2f)
                .lineTo(pageSize.getWidth() / 2 - (xOffset - 20), y).stroke();
        pdfCanvas.moveTo(pageSize.getWidth() / 2 + (xOffset - 20), y)
                .setLineWidth(1.2f)
                .lineTo(pageSize.getWidth() - xOffset, y).stroke();
    }

    public void drawHeaderText(String text) {
        float yOffset = pageSize.getTop();
        pdfCanvas.beginText()
                .setFontAndSize(font, 9)
                .setFillColor(CancerColor.getThemeColor())
                .moveText(pageSize.getWidth() / 2 - text.length() * 9 / 2, yOffset - 43);
        pdfCanvas.showText(text);
        pdfCanvas.endText();
    }

    public void close() {
        pdfCanvas.release();
    }

    public void drawStamper(Rectangle bBox, ReportBuilderContext builderContext) {
        drawStamper(bBox, builderContext, 0, 0, 90);
    }

    public void drawStamper(Rectangle bBox, ReportBuilderContext builderContext, float xOff, float yOff, float size) {
        String logoImageUrl = builderContext.getLogoImageUrl();
        String stamperUrl = builderContext.getStamperUrl();

        Image z1 = new Image(ImageDataFactory.create(CancerPainting.class.getClassLoader().getResource("image/cancer/z1.png")));
        PdfPage page = pdf.getPage(pdf.getNumberOfPages());
        float xOffset = 180;
        if (!StringUtils.isEmpty(logoImageUrl)) {
            xOffset = 240;
        }
        Rectangle rectangle = new Rectangle(
                bBox.getRight() - xOffset + xOff,
                bBox.getTop() - 5 + yOff,
                size,
                size);

        Canvas canvas = new Canvas(page, rectangle);
        z1.setOpacity(0.8f);
        canvas.add(z1).flush();
        canvas.close();


        if (!StringUtils.isEmpty(logoImageUrl)) {
            Rectangle rectangle2 = new Rectangle(
                    bBox.getRight() - 130,
                    bBox.getTop() - 5,
                    90,
                    90);
            canvas = new Canvas(page, rectangle2);
            Image z2 = null;
            try {
                Precondition.checkNotNull(stamperUrl, "签章不能为空");
                z2 = new Image(ImageDataFactory.create(stamperUrl));
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            z2.setOpacity(0.8f);
            canvas.add(z2).flush();
            canvas.close();
        }

    }
}
